//
//  TestVC1.swift
//  JTYP
//
//  Created by Groot on 2017/11/28.
//  Copyright © 2017年 inspur. All rights reserved.
//

protocol PedometerListener: class {
    func pedometerChanged(pedometer: Int)
}

class PedometerBaseVC: UIViewController {
    
    var circuView: StepProgressView?
    var timer: Timer?
    var listener: PedometerListener?
    
    // 热量
    private var quantityOfHeat: CGFloat = 0.00 {
        didSet {
            quantityOfHeatLab.countFromCurrentValue(to: quantityOfHeat)
        }
    }
    
    // 距离
    private var distance: CGFloat = 0.00 {
        didSet {
            distanceLab.countFromCurrentValue(to: distance / 1000)
            //卡路里消耗（千卡） =  体重（kg）×距离（公里）×1.036
            quantityOfHeat = distance / 1000 * 60 * 1.036
        }
    }
    
    // 显示热量
    private lazy var quantityOfHeatLab: UICountingLabel = {
        var lab = UICountingLabel()
        lab.textColor = DefaultColor.green
        lab.font = UIFont.boldSystemFont(ofSize: 21)
        lab.animationDuration = 0.5
        lab.format = "%.2f"
        lab.text = "0.00"
        return lab
    }()
    
    // 显示距离
    private lazy var distanceLab: UICountingLabel = {
        var lab = UICountingLabel()
        lab.textColor = DefaultColor.green
        lab.font = UIFont.boldSystemFont(ofSize: 21)
        lab.animationDuration = 0.5
        lab.format = "%.2f"
        lab.text = "0.00"
        return lab
    }()
    
    private lazy var backgroundImgView: UIImageView = {
        var imgView = UIImageView()
        imgView.image = UIImage.init(named: "sport_top_bg")
        return imgView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear

        view.addSubview(backgroundImgView)
        view.sendSubview(toBack: backgroundImgView)
        backgroundImgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        // 热量视图
        let v1 = UIView()
        v1.backgroundColor = .clear
        view.addSubview(v1)
        v1.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview().offset(-80)
        }
        let v1Sub0 = UILabel()
        v1Sub0.textColor = UIColorFromRGB(rgbValue: 0x333333)
        v1Sub0.font = UIFont.systemFont(ofSize: 16)
        v1Sub0.text = "热量"
        v1.addSubview(v1Sub0)
        v1Sub0.snp.makeConstraints { (make) in
            make.bottom.left.equalToSuperview()
        }
        let v1Sub1 = UILabel()
        v1Sub1.textColor = UIColorFromRGB(rgbValue: 0x333333)
        v1Sub1.font = UIFont.systemFont(ofSize: 14)
        v1Sub1.text = "/千卡"
        v1.addSubview(v1Sub1)
        v1Sub1.snp.makeConstraints { (make) in
            make.left.equalTo(v1Sub0.snp.right)
            make.right.equalToSuperview()
            make.bottom.equalTo(v1Sub0).offset(-1)
        }
        v1.addSubview(quantityOfHeatLab)
        quantityOfHeatLab.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.bottom.equalTo(v1Sub0.snp.top)
            make.centerX.equalToSuperview()
        }
        
        // 距离视图
        let v2 = UIView()
        v2.backgroundColor = .clear
        view.addSubview(v2)
        v2.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview().offset(80)
        }
        let v2Sub0 = UILabel()
        v2Sub0.textColor = UIColorFromRGB(rgbValue: 0x333333)
        v2Sub0.font = UIFont.systemFont(ofSize: 16)
        v2Sub0.text = "距离"
        v2.addSubview(v2Sub0)
        v2Sub0.snp.makeConstraints { (make) in
            make.bottom.left.equalToSuperview()
        }
        let v2Sub1 = UILabel()
        v2Sub1.textColor = UIColorFromRGB(rgbValue: 0x333333)
        v2Sub1.font = UIFont.systemFont(ofSize: 14)
        v2Sub1.text = "/公里"
        v2.addSubview(v2Sub1)
        v2Sub1.snp.makeConstraints { (make) in
            make.left.equalTo(v2Sub0.snp.right)
            make.right.equalToSuperview()
            make.bottom.equalTo(v2Sub0).offset(-1)
        }
        v2.addSubview(distanceLab)
        distanceLab.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.bottom.equalTo(v2Sub0.snp.top)
            make.centerX.equalToSuperview()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        PedometerHelper.sharedInstance.StopUpdates()
        timer?.invalidate()
        timer = nil
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if timer == nil {
            timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(update(timer:)), userInfo: nil, repeats: true)
            RunLoop.main.add(timer!, forMode: RunLoopMode.commonModes)
        }
    }
    
    @objc func update(timer: Timer) {
        PedometerHelper.sharedInstance.startUpdates {[unowned self] (data, err) in
            DispatchQueue.main.sync(execute: {[unowned self] in
                // 更新步数
                self.circuView?.currentStepNumber = data?.numberOfSteps.intValue ?? 0
                self.listener?.pedometerChanged(pedometer: data?.numberOfSteps.intValue ?? 0)
                // 更新热量和距离
                self.distance = CGFloat(data?.distance?.floatValue ?? 0.0)
            })
        }
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if isViewLoaded && circuView == nil {

            let rect = CGRect.init(x: 0, y: 0, width: self.view.frame.width - 20, height: self.view.frame.height - 50)
            circuView = StepProgressView.init(frame: rect)
            circuView?.center = CGPoint.init(x: self.view.bounds.size.width / 2, y: self.view.bounds.size.height / 2 - 25)
            circuView?.target = 10000
            self.view.addSubview(circuView!)
        }
    }
}
